$(() => {
   // 获取登录页文章
   {
      let id
      const agree = $('.Layout_Center1 .agree')
      $('.Layout_Center1 .agree-btn').on('click', function () {
         $.get('/writing/agree', {
            id
         }, (res) => {
            if (res.code == 200)
               agree.text(+agree.text() + 1)
         })
      })
      $.get('/writing', (res) => {
         $('.Layout_Center1 h4').text(res.title)
         $('.Layout_Center1 p').text(res.main)
         agree.text(res.agree)
         id = res.id
      })
   }
   // 登陆
   {
      const mailboxInput = $('.Sign_in .Touch_plate_input_mailbox input')
      const parsswordInput = $('.Sign_in .Touch_plate_input_password input')
      let data
      function input() {
         data = {
            email: mailboxInput.val(),
            password: parsswordInput.val()
         }
         if (data.email && data.password) {
            $('.Sign_in .Touch_plate_input_button').addClass('y')
         } else {
            $('.Sign_in .Touch_plate_input_button').removeClass('y')
         }
      }
      parsswordInput.on('input', input)
      mailboxInput.on('input', input)
      $('.Sign_in .Touch_plate_input_button').on('click', function () {
         if ($(this).hasClass('y')) {
            $.post('/login', data, res => {
               if (res.code == 200) {
                  localStorage.setItem('Token', res.token)
                  // if (/\/login^/.exec(document.referrer)) {
                     location.href = '/'
                  // } else {
                     // location.href = document.referrer
                  // }
               } else if (res.code == 322) {
                  $('.Sign_in .tips').text('账号或密码错误')
               }
            })
         }
      })
   }
   // 注册
   {
      const emailReg = /^\S+@.+(.com|.net)$/i
      const nameReg = /^\S{2,16}$/i
      const nicknameInput = $('.register .Touch_plate_input_mailbox input')
      const passwordInput = $('.register .Touch_plate_input_password input')
      const emailInput = $('.register .Touch_plate_input_email input')
      const codeInput = $('.register .Touch_plate_input_code input')
      const sendcode = $('.sendcode')
      let id
      sendcode.on('click', function () {
         if (id) returnv
         let time = 60
         sendcode.text(`${time}秒`)
         id = setInterval(() => {
            time--
            sendcode.text(`${time}秒`)
            if (!time) {
               clearInterval(id)
               id = 0
               sendcode.text('获取验证码')
            }
         }, 1000)
         const email = emailInput.val()
         if (emailReg.test(email)) {
            $.post('/authcode', {
               email
            }, res => {
               console.log(res);
               if (res.code == 200) {
                  console.log(res);
               } else {
                  console.log(res);
               }
            })
         }
      })
      $('.register .Touch_plate_input_button').on('click', function () {
         const data = {
            nickname: nicknameInput.val(),
            password: passwordInput.val(),
            email: emailInput.val(),
            authcode: codeInput.val()
         }
         $.post('/register', data, res => {
            if (res.code == 200) {
               localStorage.setItem('Token', res.token)
               location.href = '/'
            } else {
               $('.register .tips').text(res.msg)
            }
         })
      })
   }
})